#include "ccarRequestPassenger.h"
#include <QDataStream>
#include <QTcpSocket>
#include "protocol/CarRequestPassenger.h"
#include "protocol/srvCarRequestPassengersAnsToCar.h"
#include "protocol/srvbroadcast.h"
CCarRequestPassengers::CCarRequestPassengers(QObject *parent)
    :INetWorking(parent)
{
}

CCarRequestPassengers::~CCarRequestPassengers()
{
}

void CCarRequestPassengers::prepareWriteData(QByteArray* block, void* param)
{
    // we cannot discrete this definition to the outside of the block,
    // and pass the reference to the funtion, or it will cause some
    // unknown problems, such as wrong streams. Maybe it is a bug of QT.
    QDataStream out(block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);


    protocal::CarRequestPassenger* pRequestPassenger = (protocal::CarRequestPassenger*)param;

    // output the head information
    out << (qint32)(pRequestPassenger->source) \
        << (qint32)(pRequestPassenger->car_command) \
        << (qint32)(pRequestPassenger->size);

    // output the body information

    char * temp = new char [10];
    // we must do so, because the constructor of QString(char*) will truncate the
    // remaining space after '\0', and we need them to occupy the space.
    memcpy(temp, pRequestPassenger->carID, 10);
    out << temp;

    delete [] temp;
}

bool CCarRequestPassengers::prepareReadData(QTcpSocket *pTcpSocket, protocal::BasePtc *basePtc)
{
    QDataStream in(pTcpSocket);

    if (   basePtc->source != protocal::SERVER\
        || basePtc->car_command != protocal::SRV_BROADCAST
        || basePtc->size != sizeof(protocal::SrvBroadcast)+4)
    {
        return false;
    }

    char * temp   = new char[10];
    // recieve data, from srvCarRequestPassengersAnsToCar.h
    //in >> client_id;
    in >> temp;
    client_id = QLatin1String(temp);
    delete [] temp;
    // GPS information should be accepted here


    in >> align;

    in >> start_point.lat;
    //start_point.lat = qFromLittleEndian(start_point.lat);
    in >> start_point.lon;
    //start_point.lon = qFromLittleEndian(start_point.lon);
    in >> start_point.time;
    in >> terminal_point.lat;
    in >> terminal_point.lon;
    in >> terminal_point.time;

    //in
    in >> timeout;
    in >> honesty_degree;

    formResult();

    return true;
}

void CCarRequestPassengers::formResult()
{
    resultMap.insert(QString("client_id"), client_id);



    resultMap.insert(QString("startGPS.lat"), QString::number(start_point.lat));
    resultMap.insert(QString("startGPS.lon"), QString::number(start_point.lon));
    resultMap.insert(QString("startGPS.time"), QString::number(start_point.time));

    resultMap.insert(QString("endGPS.lat"), QString::number(terminal_point.lat));
    resultMap.insert(QString("endGPS.lon"), QString::number(terminal_point.lon));
    resultMap.insert(QString("endGPS.time"), QString::number(terminal_point.time));

    resultMap.insert(QString("timeout"), QString::number((short)timeout) );
    resultMap.insert(QString("honesty_degree"), QString::number((short)honesty_degree) );
}
